<?php

class User {

    public function __construct($user_id) {
        $this->user_id = $user_id;
        $query = "SELECT * FROM students_login where id='$user_id'";
        $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
        $row = mysql_fetch_assoc($result);
        $this->username = $row['username'];
        $this->phone_number = $row['phone_number'];
    }

}

function hasQuestions($quiz_id, $user_id) {
    $query = "SELECT * FROM questions WHERE quiz_id='$quiz_id' AND
    question_id NOT IN (SELECT question_id FROM user_answers WHERE user_id='$user_id')
    ORDER BY question_id ASC LIMIT 0,1";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    if (mysql_num_rows($result) == 0) {
        return FALSE;
    } else {
        return TRUE;
    }
}

function enrolled($course_id, $user_id) {
    $query = "SELECT * FROM enrollment WHERE user_id = '$user_id' and course_id='$course_id'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    return mysql_num_rows($result) > 0;
}

function numofLessons($course_id) {
    $query = "SELECT * FROM lessons where course='$course_id' order by 'date_added' desc";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    $num_lessons = mysql_num_rows($result);
    return $num_lessons;
}

function numOflessonsDone($course_id, $user_id) {
    $query = "SELECT * FROM user_lessons where user_id='$user_id' AND lesson_id in (SELECT id FROM lessons where course='$course_id')";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    $num_done = mysql_num_rows($result);
    return $num_done;
}

function courseDone($course_id, $user_id) {
    $num_lessons = numofLessons($course_id);
    $num_done = numOflessonsDone($course_id, $user_id);
    return $num_done == $num_lessons;
}

function courseQuizesDone($course_id, $user_id) {
    $query = "SELECT * FROM quizes where course_id='$course_id' order by date_added desc";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    if (mysql_num_rows($result) == 0) {
        return FALSE;
    } else {
        while ($row = mysql_fetch_assoc($result)) {
            extract($row);
            if (hasQuestions($quiz_id, $user_id)) {//if any of the quizes still has questions, then the course is not done
                return FALSE;
            }
        }
        return TRUE;
    }
}

function enrollCourse($user_id, $course_id) {//FETCH ENNROLLED OR NOT-ENROLLED COURSES
    $coursesStr = "";
    $query = "INSERT INTO enrollment SET course_id='$course_id',user_id = '$user_id'";
    if (mysql_query($query)) {
        addNotification("You have been successfully enrolled in -" . getCourse($course_id) . "-, Go Home>Courses to access contents", "Admin",$user_id);
    }
}

function addNotification($message, $from, $user_id) {
    $query = "INSERT INTO user_notifications SET user_id='$user_id', message='$message',
            notif_from='$from'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
}

function getCourse($course_id) {
    $query = "SELECT course FROM courses where id='$course_id'";
    $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
    $row = mysql_fetch_array($result);
    $course = $row['course'];
    return $course;
}

function courseStatus($course_id, $user_id) {
    if (enrolled($course_id, $user_id)) {
        if (courseQuizesDone($course_id, $user_id)) {
            return "DONE";
        } else if (courseDone($course_id, $user_id)) {
            return "QUIZ PENDING";
        } else if (numOflessonsDone($course_id, $user_id) > 0) {
            return "STARTED";
        } else {
            return "NOT STARTED";
        }
    } else {
        $enroll_url = "";
        if (isset($_SESSION['type']) && $_SESSION['type'] == "admin") {
            $enroll_url = "<a href='staff_courses.php?staff_id=$user_id&course_id=$course_id&enroll=1'>Enroll</a>";
        }
        return "NOT ENROLLED $enroll_url";
    }
}
